package org.jeecg.modules.qyxx.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import org.jeecg.modules.qyxx.entity.*;
import org.jeecg.modules.qyxx.entity.req.BgNsrjbxxReq;
import org.jeecg.modules.qyxx.entity.req.SsjcSssjDTO;
import org.jeecg.modules.qyxx.entity.resp.BgQyjbxxBgVo;
import org.jeecg.modules.qyxx.mapper.TycLsgsxxLsmcMapper;
import org.jeecg.modules.qyxx.service.*;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 历史工商信息_历史名称
 * @Author: jeecg-boot
 * @Date: 2022-02-18
 * @Version: V1.0
 */
@Service
//@DS("fk_oracle")
public class TycLsgsxxLsmcServiceImpl extends MppServiceImpl<TycLsgsxxLsmcMapper, TycLsgsxxLsmc> implements ITycLsgsxxLsmcService {
    @Autowired
    private ITycLsgsxxDjglbService tycLsgsxxDjglbService;
    @Autowired
    private ITycLsgsxxLsfrlbService tycLsgsxxLsfrlbService;
    @Autowired
    private ITycLsgsxxSyfwService tycLsgsxxSyfwService;
    @Autowired
    private ITycLsgsxxZcdzlbService tycLsgsxxZcdzlbService;
    @Autowired
    private ITycLsgsxxZczblbService tycLsgsxxZczblbService;
    @Autowired
    private ITycLsgsxxFzjgmclbService tycLsgsxxFzjgmclbService;
    @Autowired
    private ITycLsgsxxGslxlbService tycLsgsxxGslxlbService;
    @Autowired
    private ITycLsgsxxJyqxlbService tycLsgsxxJyqxlbService;
    @Autowired
    private ITycLsgsxxTyshxydmlbService tycLsgsxxTyshxydmlbService;
    @Autowired
    private ITycLsgsxxZchlbService tycLsgsxxZchlbService;


    @Override
    public List<BgQyjbxxBgVo> qyjbxxBg(BgNsrjbxxReq req) {
        List<BgQyjbxxBgVo> list = new ArrayList<>();
        DateUtils utils = DateUtils.getInstance();
        String djxh = req.getDjxh();
        List<String> tjnds = utils.nearlyThreeYears(req.getKssj(), req.getJssj());
        List<TycLsgsxxLsmc> lsmcList = baseMapper.listByShxydm(djxh);
        for (TycLsgsxxLsmc tycLsgsxxLsmc : lsmcList) {
            Date bgsj = new Date(tycLsgsxxLsmc.getTime());
            String year = utils.getYearToStr(bgsj);
            if (tjnds.contains(year)) {
                BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
                vo.setBgsj(bgsj);
                vo.setBgsx("历史名称");
                vo.setBgxq(tycLsgsxxLsmc.getName());
                list.add(vo);
            }
        }

        List<TycLsgsxxDjglb> djglbList = tycLsgsxxDjglbService.listByShxydm(djxh);
        for (TycLsgsxxDjglb tycLsgsxxDjglb : djglbList) {
            Date bgsj = new Date(tycLsgsxxDjglb.getTime());
            String year = utils.getYearToStr(bgsj);
            if (tjnds.contains(year)) {
                BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
                vo.setBgsj(bgsj);
                vo.setBgsx("董监高");
                vo.setBgxq(tycLsgsxxDjglb.getName());
                list.add(vo);
            }
        }

        List<TycLsgsxxLsfrlb> lsfrlbList = tycLsgsxxLsfrlbService.listByShxydm(djxh);
        for (TycLsgsxxLsfrlb tycLsgsxxLsfrlb : lsfrlbList) {
            Date bgsj = new Date(tycLsgsxxLsfrlb.getTime());
            String year = utils.getYearToStr(bgsj);
            if (tjnds.contains(year)) {
                BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
                vo.setBgsj(bgsj);
                vo.setBgsx("历史法人");
                vo.setBgxq(tycLsgsxxLsfrlb.getName());
                list.add(vo);
            }
        }

        List<TycLsgsxxSyfw> syfwList = tycLsgsxxSyfwService.listByShxydm(djxh);
        for (TycLsgsxxSyfw tycLsgsxxSyfw : syfwList) {
            Date bgsj = new Date(tycLsgsxxSyfw.getTime());
            String year = utils.getYearToStr(bgsj);
            if (tjnds.contains(year)) {
                BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
                vo.setBgsj(bgsj);
                vo.setBgsx("商业范围");
                vo.setBgxq(tycLsgsxxSyfw.getName());
                list.add(vo);
            }
        }

        List<TycLsgsxxZcdzlb> zcdzlbList = tycLsgsxxZcdzlbService.listByShxydm(djxh);
        for (TycLsgsxxZcdzlb tycLsgsxxZcdzlb : zcdzlbList) {
            Date bgsj = new Date(tycLsgsxxZcdzlb.getTime());
            String year = utils.getYearToStr(bgsj);
            if (tjnds.contains(year)) {
                BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
                vo.setBgsj(bgsj);
                vo.setBgsx("注册地址");
                vo.setBgxq(tycLsgsxxZcdzlb.getName());
                list.add(vo);
            }
        }

        List<TycLsgsxxZczblb> zczblbList = tycLsgsxxZczblbService.listByShxydm(djxh);
        for (TycLsgsxxZczblb tycLsgsxxZczblb : zczblbList) {
            Date bgsj = new Date(tycLsgsxxZczblb.getTime());
            String year = utils.getYearToStr(bgsj);
            if (tjnds.contains(year)) {
                BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
                vo.setBgsj(bgsj);
                vo.setBgsx("注册资本");
                vo.setBgxq(tycLsgsxxZczblb.getName());
                list.add(vo);
            }
        }

        List<TycLsgsxxFzjgmclb> fzjgmclbList = tycLsgsxxFzjgmclbService.listByShxydm(djxh);
        for (TycLsgsxxFzjgmclb tycLsgsxxFzjgmclb : fzjgmclbList) {
            Date bgsj = new Date(tycLsgsxxFzjgmclb.getTime());
            String year = utils.getYearToStr(bgsj);
            if (tjnds.contains(year)) {
                BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
                vo.setBgsj(bgsj);
                vo.setBgsx("分支机构");
                vo.setBgxq(tycLsgsxxFzjgmclb.getName());
                list.add(vo);
            }
        }

        List<TycLsgsxxGslxlb> gslxlbList = tycLsgsxxGslxlbService.listByShxydm(djxh);
        for (TycLsgsxxGslxlb tycLsgsxxGslxlb : gslxlbList) {
            Date bgsj = new Date(tycLsgsxxGslxlb.getTime());
            String year = utils.getYearToStr(bgsj);
            if (tjnds.contains(year)) {
                BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
                vo.setBgsj(bgsj);
                vo.setBgsx("公司类型");
                vo.setBgxq(tycLsgsxxGslxlb.getName());
                list.add(vo);
            }
        }

        List<TycLsgsxxJyqxlb> jyqxlbList = tycLsgsxxJyqxlbService.listByShxydm(djxh);
        for (TycLsgsxxJyqxlb tycLsgsxxJyqxlb : jyqxlbList) {
            Date bgsj = new Date(tycLsgsxxJyqxlb.getTime());
            String year = utils.getYearToStr(bgsj);
            if (tjnds.contains(year)) {
                BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
                vo.setBgsj(bgsj);
                vo.setBgsx("经营期限");
                vo.setBgxq(tycLsgsxxJyqxlb.getName());
                list.add(vo);
            }
        }

        List<TycLsgsxxTyshxydmlb> tyshxydmlbList = tycLsgsxxTyshxydmlbService.listByShxydm(djxh);
        for (TycLsgsxxTyshxydmlb tycLsgsxxTyshxydmlb : tyshxydmlbList) {
            Date bgsj = new Date(tycLsgsxxTyshxydmlb.getTime());
            String year = utils.getYearToStr(bgsj);
            if (tjnds.contains(year)) {
                BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
                vo.setBgsj(bgsj);
                vo.setBgsx("统一社会信用代码");
                vo.setBgxq(tycLsgsxxTyshxydmlb.getName());
                list.add(vo);
            }
        }

        List<TycLsgsxxZchlb> zchlbList = tycLsgsxxZchlbService.listByShxydm(djxh);
        for (TycLsgsxxZchlb tycLsgsxxZchlb : zchlbList) {
            Date bgsj = new Date(tycLsgsxxZchlb.getTime());
            String year = utils.getYearToStr(bgsj);
            if (tjnds.contains(year)) {
                BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
                vo.setBgsj(bgsj);
                vo.setBgsx("注册号");
                vo.setBgxq(tycLsgsxxZchlb.getName());
                list.add(vo);
            }
        }
        return list;
    }

    @Override
    public List<BgQyjbxxBgVo> gsxxbg(SsjcSssjDTO req) {
        List<BgQyjbxxBgVo> list = new ArrayList<>();
        DateUtils utils = DateUtils.getInstance();
        String tjnd = req.getTjnd();
        String djxh = req.getShxydm();
        // List<String> tjnds = utils.nearlyThreeYears(req.getKssj(), req.getJssj());
        List<TycLsgsxxLsmc> lsmcList = baseMapper.listByShxydmAndTjnd(djxh, tjnd);
        for (TycLsgsxxLsmc tycLsgsxxLsmc : lsmcList) {
            Date bgsj = new Date(tycLsgsxxLsmc.getTime());
            //String year = utils.getYearToStr(bgsj);
            //if (tjnds.contains(year)) {
            BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
            vo.setBgsj(bgsj);
            vo.setBgsx("历史名称");
            vo.setBgxq(tycLsgsxxLsmc.getName());
            list.add(vo);
            // }
        }

        List<TycLsgsxxDjglb> djglbList = tycLsgsxxDjglbService.listByShxydmByTjnd(djxh, tjnd);
        for (TycLsgsxxDjglb tycLsgsxxDjglb : djglbList) {
            Date bgsj = new Date(tycLsgsxxDjglb.getTime());
            // String year = utils.getYearToStr(bgsj);
            //if (tjnds.contains(year)) {
            BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
            vo.setBgsj(bgsj);
            vo.setBgsx("董监高");
            vo.setBgxq(tycLsgsxxDjglb.getName());
            list.add(vo);
            //}
        }

        List<TycLsgsxxLsfrlb> lsfrlbList = tycLsgsxxLsfrlbService.listByShxydmAndTjnd(djxh,tjnd);
        for (TycLsgsxxLsfrlb tycLsgsxxLsfrlb : lsfrlbList) {
            Date bgsj = new Date(tycLsgsxxLsfrlb.getTime());
            // String year = utils.getYearToStr(bgsj);
            // if (tjnds.contains(year)) {
            BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
            vo.setBgsj(bgsj);
            vo.setBgsx("历史法人");
            vo.setBgxq(tycLsgsxxLsfrlb.getName());
            list.add(vo);
            // }
        }

        List<TycLsgsxxSyfw> syfwList = tycLsgsxxSyfwService.listByShxydmAndTjnd(djxh,tjnd);
        for (TycLsgsxxSyfw tycLsgsxxSyfw : syfwList) {
            Date bgsj = new Date(tycLsgsxxSyfw.getTime());
            //String year = utils.getYearToStr(bgsj);
            // if (tjnds.contains(year)) {
            BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
            vo.setBgsj(bgsj);
            vo.setBgsx("商业范围");
            vo.setBgxq(tycLsgsxxSyfw.getName());
            list.add(vo);
            //}
        }

        List<TycLsgsxxZcdzlb> zcdzlbList = tycLsgsxxZcdzlbService.listByShxydmAndTjnd(djxh,tjnd);
        for (TycLsgsxxZcdzlb tycLsgsxxZcdzlb : zcdzlbList) {
            Date bgsj = new Date(tycLsgsxxZcdzlb.getTime());
            //String year = utils.getYearToStr(bgsj);
            //  if (tjnds.contains(year)) {
            BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
            vo.setBgsj(bgsj);
            vo.setBgsx("注册地址");
            vo.setBgxq(tycLsgsxxZcdzlb.getName());
            list.add(vo);
            // }
        }

        List<TycLsgsxxZczblb> zczblbList = tycLsgsxxZczblbService.listByShxydmAndTjnd(djxh,tjnd);
        for (TycLsgsxxZczblb tycLsgsxxZczblb : zczblbList) {
            Date bgsj = new Date(tycLsgsxxZczblb.getTime());
            // String year = utils.getYearToStr(bgsj);
            // if (tjnds.contains(year)) {
            BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
            vo.setBgsj(bgsj);
            vo.setBgsx("注册资本");
            vo.setBgxq(tycLsgsxxZczblb.getName());
            list.add(vo);
            //}
        }

        List<TycLsgsxxFzjgmclb> fzjgmclbList = tycLsgsxxFzjgmclbService.listByShxydmAndTjnd(djxh,tjnd);
        for (TycLsgsxxFzjgmclb tycLsgsxxFzjgmclb : fzjgmclbList) {
            Date bgsj = new Date(tycLsgsxxFzjgmclb.getTime());
            //String year = utils.getYearToStr(bgsj);
            //if (tjnds.contains(year)) {
            BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
            vo.setBgsj(bgsj);
            vo.setBgsx("分支机构");
            vo.setBgxq(tycLsgsxxFzjgmclb.getName());
            list.add(vo);
            // }
        }

        List<TycLsgsxxGslxlb> gslxlbList = tycLsgsxxGslxlbService.listByShxydmAndTjnd(djxh,tjnd);
        for (TycLsgsxxGslxlb tycLsgsxxGslxlb : gslxlbList) {
            Date bgsj = new Date(tycLsgsxxGslxlb.getTime());
            // String year = utils.getYearToStr(bgsj);
            //if (tjnds.contains(year)) {
            BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
            vo.setBgsj(bgsj);
            vo.setBgsx("公司类型");
            vo.setBgxq(tycLsgsxxGslxlb.getName());
            list.add(vo);
            //}
        }

        List<TycLsgsxxJyqxlb> jyqxlbList = tycLsgsxxJyqxlbService.listByShxydmAndTjnd(djxh,tjnd);
        for (TycLsgsxxJyqxlb tycLsgsxxJyqxlb : jyqxlbList) {
            Date bgsj = new Date(tycLsgsxxJyqxlb.getTime());
            //String year = utils.getYearToStr(bgsj);
            //if (tjnds.contains(year)) {
            BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
            vo.setBgsj(bgsj);
            vo.setBgsx("经营期限");
            vo.setBgxq(tycLsgsxxJyqxlb.getName());
            list.add(vo);
            //  }
        }

        List<TycLsgsxxTyshxydmlb> tyshxydmlbList = tycLsgsxxTyshxydmlbService.listByShxydmAndTjnd(djxh,tjnd);
        for (TycLsgsxxTyshxydmlb tycLsgsxxTyshxydmlb : tyshxydmlbList) {
            Date bgsj = new Date(tycLsgsxxTyshxydmlb.getTime());
            // String year = utils.getYearToStr(bgsj);
            //if (tjnds.contains(year)) {
            BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
            vo.setBgsj(bgsj);
            vo.setBgsx("统一社会信用代码");
            vo.setBgxq(tycLsgsxxTyshxydmlb.getName());
            list.add(vo);
            //}
        }

        List<TycLsgsxxZchlb> zchlbList = tycLsgsxxZchlbService.listByShxydmAndTjnd(djxh,tjnd);
        for (TycLsgsxxZchlb tycLsgsxxZchlb : zchlbList) {
            Date bgsj = new Date(tycLsgsxxZchlb.getTime());
            // String year = utils.getYearToStr(bgsj);
            //if (tjnds.contains(year)) {
            BgQyjbxxBgVo vo = new BgQyjbxxBgVo();
            vo.setBgsj(bgsj);
            vo.setBgsx("注册号");
            vo.setBgxq(tycLsgsxxZchlb.getName());
            list.add(vo);
            // }
        }
        return list;
    }
}
